package myoa.tool

import org.codehaus.groovy.grails.validation.UrlConstraint
import org.codehaus.groovy.grails.validation.routines.UrlValidator
import org.codehaus.groovy.grails.web.util.WebUtils
import org.joda.time.LocalDate

import javax.servlet.http.HttpServletRequest


class StringTool {
    static String addUrlProtocol(String url) {
        if (url == null || url.isEmpty()) {
            return url
        }

        def u = UrlValidator.getInstance()
        if (!u.isValid(url)) {
            url = "http://${url}"
        }
        return url
    }

    /**
     *
     * @param localDate excel data type
     * @return
     */
    static Date parseExcelDate(LocalDate localDate) {
        localDate.toDate()
    }

    static int paginateMax(String max) {
        int re = 10
        if (max?.isInteger()) {
            re = Math.min(Integer.valueOf(max), 30)
        }
        return re
    }
    /**
     *
     * @param controllerName
     * @param match
     * @return get current page css for home menu
     */
    static String curPage(String controllerName, String... match) {
        def flag = controllerName in match
        flag ? 'current_page_item' : ''
    }
}
